package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TBaseRace;
import com.nice.nice201110.service.TbaseRaceService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/race")
public class TbaseRaceComtorller {

    @Autowired
    TbaseRaceService tbaseRaceService;

    private Map<String,Object> codeMes = new HashMap<>();
    /**
     *  查看信息的基本状况
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> getlist(HttpServletRequest request, @RequestBody(required = false) BaseVo baseVo){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(baseVo!=null){
            baseVo.setUservo(userVo);
        }else{
            baseVo = new BaseVo();
            baseVo.setUservo(userVo);
        }
        List<TBaseRace> tBaseRaces = tbaseRaceService.getList(baseVo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tBaseRaces);
        return codeMes;

    }
    /**
     *   查询单一基本信息
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> getOne(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TBaseRace tBaseRace = tbaseRaceService.getOne(userVo,id);
        if(tBaseRace!=null){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseRace);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 基本信息 更新 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT,value = "/update")
    public Map<String,Object> update(HttpServletRequest request,@RequestBody TBaseRace tBaseRace){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseRaceService.updateTbaseRace(userVo,tBaseRace);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseRace);
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 基本信息插入 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value="/insert")
    public Map<String,Object> insert(HttpServletRequest request,TBaseRace tBaseRace){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseRaceService.insertTbaseRace(userVo,tBaseRace);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseRace);
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 删除信息 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/delete")
    public Map<String,Object> delete(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseRaceService.deleteTbaseRace(userVo,id);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        }
        codeMes.put("code", NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 删除信息s */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/deletes")
    public Map<String,Object> deletes(HttpServletRequest request,@RequestParam(name = "ids",required=true) String ids){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseRaceService.deletesTbaseRace(userVo,ids);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
}
